package com.github.phoenics.militia.bazookaFx.controller;

import com.github.phoenics.militia.bazookaFx.domain.entity.AppLink;
import com.github.phoenics.militia.bazookaFx.generator.config.QueryTemplate;
import com.github.phoenics.militia.bazookaFx.model.AppLinkModel;
import com.github.phoenics.militia.bazookaFx.service.AppLinkService;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author Phoenics Chow
 * on 2019年02月12日
 */
@FXMLController
public class EditGenLinkController implements Initializable {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(EditGenLinkController.class);

    public static final AppLinkModel appLinkModel=new AppLinkModel();
    public static final IntegerProperty openCount=new SimpleIntegerProperty(0);
    public static final StringProperty dbType=new SimpleStringProperty();

    @FXML
    TextField appname ;
    @FXML
    TextField driver ;
    @FXML
    TextField dburl ;
    @FXML
    TextField username ;
    @FXML
    TextField password ;
    @FXML
    TextField schemaName ;
    @FXML
    Button sub;
    @Autowired
    AppLinkService appLinkService;
    @Autowired
    MainLaunchController mainLaunchController;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        openCount.addListener((ob,o,n)-> sub.setDisable(true));
        appname.textProperty().bindBidirectional(appLinkModel.appNameProperty());
        driver.textProperty().bindBidirectional(appLinkModel.driverClassNameProperty());
        dburl.textProperty().bindBidirectional(appLinkModel.urlProperty());
        username.textProperty().bindBidirectional(appLinkModel.usernameProperty());
        password.textProperty().bindBidirectional(appLinkModel.passwordProperty());
        schemaName.textProperty().bindBidirectional(appLinkModel.schemaNameProperty());
        driver.textProperty().addListener((ob,o,n)-> sub.setDisable(true));
        dburl.textProperty().addListener((ob,o,n)-> sub.setDisable(true));
        username.textProperty().addListener((ob,o,n)-> sub.setDisable(true));
        password.textProperty().addListener((ob,o,n)-> sub.setDisable(true));
        schemaName.textProperty().addListener((ob,o,n)-> sub.setDisable(true));
    }
    @FXML
    private void submit(ActionEvent event){
        AppLink appLink=appLinkModel.takeObjectWarpper(new AppLink());
        appLinkService.save(appLink);
        mainLaunchController.reloadAppTree();
    }
    @FXML
    private void cancel(ActionEvent event){
        ((Stage)((Button)event.getTarget()).getScene().getWindow()).close();
    }
    @FXML
    private void testlink(ActionEvent event){
        try{
            QueryTemplate queryTemplate=new QueryTemplate(appLinkModel.takeObjectWarpper(new AppLink()));
            if(!queryTemplate.testLink()){
                throw new Exception();
            }
            takeMessageOK();
            sub.setDisable(false);
        }catch (Exception e){
            takeMessageERROR();
        }

    }
    private void takeMessageERROR(){
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("ERROR Dialog");
        alert.setHeaderText("测试连接数据库");
        alert.setContentText("数据库连接失败！！！");
        alert.showAndWait();
    }
    private void takeMessageOK(){
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("INFORMATION Dialog");
        alert.setHeaderText("测试连接数据库");
        alert.setContentText("数据库连接成功！！！");
        alert.showAndWait();
    }

}
